<?php
/**
 * Created by PhpStorm.
 * User: lauwen
 * Date: 2017/10/24
 * Time: 19:39
 *
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘
 *             │ ─┤ ─┤       │ ─┤ ─┤
 *             └──┴──┘       └──┴──┘
 *                 神兽保佑
 *                 代码无BUG!
 *"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”- Seymour Cray
 */
namespace WifiSwitch\Controller;
use Think\Controller;

class UserController extends Controller{
    /**
     * Login page
     */
    public function login(){
        $isMobile = $this->is_mobile();
        if($isMobile){
            $this->display("mobile_login");
        }else{
            $this->display();
        }
    }
    public function mobile_login(){
//        R('User/loginStatusCheck',array(1));
        $this->display();
    }


    /**
     * user login info check
     */
    public function loginCheck(){
        $userName = $_POST['username'];
        $passWord = $_POST['password'];

        if(isset($userName) && isset($passWord) && !empty($passWord) && !empty($userName)){
            $User = M('user_info');
            $userInfo = $User -> where(array("name" => $userName,"pwd" => $passWord)) -> find();
//            dump($userInfo);die;
            if($userInfo){
                $_SESSION['WifiPagesLoginStatus'] = $userName;
                $_SESSION['WifiPagesUserGrade'] = $userInfo['usergrade'];
                $_SESSION['manageRegin']= $userInfo['manageregin'];
//                dump($_SESSION);die;
                $this->success("登录成功，跳转中……",U("WifiSwitch/Index/index"),3);
            }else{
                $this -> error("密码或用户名错误，请重新登录……");
            }
        }else{
            $this -> error("登录数据提交失败，请重新登录……");
        }
    }
    public function loginStatusCheck($grade){
        if((isset($_SESSION['WifiPagesLoginStatus'])) && ($_SESSION['WifiPagesLoginStatus'] != '')){
            if($_SESSION['WifiPagesUserGrade'] <= $grade){
                return true;
            }else{
                $this->error('你无权访问该页面');
            }
        }else{
            $this->error('请登录……',U('User/login'),3);
        }
    }

    public function exitLogin(){
        $_SESSION['WifiPagesLoginStatus'] = null;
        $_SESSION['WifiPagesUserGrade'] = null;
        $this->success('已退出登录！','login',3);
    }

    public function is_mobile(){
        $user_agent = $_SERVER['HTTP_USER_AGENT'];

        $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
        $is_mobile = false;
        foreach ($mobile_agents as $device) {//这里把值遍历一遍，用于查找是否有上述字符串出现过
            if (stristr($user_agent, $device)) { //stristr 查找访客端信息是否在上述数组中，不存在即为PC端。
                $is_mobile = true;
                break;
            }
        }
        return $is_mobile;
    }

}


